package com.buddies.lottery.utils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

    public static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

    public static final DecimalFormat COUNT_DOWN_FORMAT = new DecimalFormat("00");

    public static String format(long time) {
        return SIMPLE_DATE_FORMAT.format(time);
    }

    public static Date parse(String datetime) {
        Date date = null;
        try {
            date = SIMPLE_DATE_FORMAT.parse(datetime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String formatDuration(long seconds) {
        if (seconds < 0) seconds = 0;
        if (seconds > 60 * 60) {
            return String.format("%s:%s:%s", COUNT_DOWN_FORMAT.format(seconds / (60 * 60)), COUNT_DOWN_FORMAT.format(seconds % (60 * 60) / 60), COUNT_DOWN_FORMAT.format(seconds % 60));
        } else {
            return String.format("%s:%s", COUNT_DOWN_FORMAT.format(seconds / 60), COUNT_DOWN_FORMAT.format(seconds % 60));
        }
    }

}
